#!/bin/bash

# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0

set -e
set -o pipefail

# regexes for packages to exclude from unit test
excluded_packages=(
    "/integration(/|$)"
)

# regexes for packages that must be run serially
serial_packages=(
    "github.com/hyperledger/fabric/gossip"
)

# packages which need to be tested with build tag pluginsenabled
plugin_packages=(
    "github.com/hyperledger/fabric/core/scc"
)

# packages which need to be tested with build tag pkcs11
pkcs11_packages=(
    "github.com/hyperledger/fabric/bccsp/..."
)

# packages that are only tested when they (or their deps) change
conditional_packages=(
    "github.com/hyperledger/fabric/gossip/..."
)

# obtain packages changed since some git refspec
packages_diff() {
    git -C "${GOPATH}/src/github.com/hyperledger/fabric" diff --no-commit-id --name-only -r "${1:-HEAD}" |
        grep '.go$' | \
        sed 's%/[^/]*$%%' | sort -u | \
        awk '{print "github.com/hyperledger/fabric/"$1}'
}

join_by() {
    local IFS="$1"; shift; echo "$*"
}

# "go list" packages and filter out excluded packages
list_and_filter() {
    local excluded filter

    excluded=("${excluded_packages[@]}")
    if [ "${#conditional_packages[@]}" -ne 0 ]; then
        while IFS=$'\n' read -r pkg; do [ -n "$pkg" ] && excluded+=("$pkg"); done < <(go list -f '{{ .ImportPath }}$' "${conditional_packages[@]}")
    fi

    filter=$(join_by '|' "${excluded[@]}")
    if [ -n "$filter" ]; then
        go list "$@" 2>/dev/null | grep -Ev "${filter}" || true
    else
        go list "$@" 2>/dev/null
    fi
}

# list conditional packages that have been changed
list_and_filter_conditional() {
    local -a additional_packages=()

    if [ "${#conditional_packages[@]}" -ne 0 ]; then
        local changed_packages
        changed_packages=$(packages_diff)
        for pkg in $(go list "${conditional_packages[@]}"); do
            local dep_regexp
            dep_regexp="$(go list -f '{{ join .Deps "$|" }}' "$pkg")"
            echo "${changed_packages}" | grep -qE "$dep_regexp" && additional_packages+=("$pkg")
            echo "${changed_packages}" | grep -qE "$pkg\$" && additional_packages+=("$pkg")
        done
    fi

    join_by $'\n' "${additional_packages[@]}"
}

# remove packages that must be tested serially
parallel_test_packages() {
    local filter
    filter=$(join_by '|' "${serial_packages[@]}")
    if [ -n "$filter" ]; then
        join_by $'\n' "$@" | grep -Ev "$filter" || true
    else
        join_by $'\n' "$@"
    fi
}

# get packages that must be tested serially
serial_test_packages() {
    local filter
    filter=$(join_by '|' "${serial_packages[@]}")
    if [ -n "$filter" ]; then
        join_by $'\n' "$@" | grep -E "$filter" || true
    else
        join_by $'\n' "$@"
    fi
}

# "go test" the provided packages. Packages that are not present in the serial package list
# will be tested in parallel
run_tests() {
    local -a flags=("-cover")
    if [ -n "${VERBOSE}" ]; then
        flags+=("-v")
    fi

    local -a race_flags=()
    if [[ "$(uname -m)" == "x86_64" ]]; then
        export GORACE=atexit_sleep_ms=0 # reduce overhead of race
        race_flags+=("-race")
    fi

    GO_TAGS=${GO_TAGS## }
    [ -n "$GO_TAGS" ] && echo "Testing with $GO_TAGS..."

    time {
        local -a serial
        while IFS=$'\n' read -r pkg; do [ -n "$pkg" ] && serial+=("$pkg"); done < <(serial_test_packages "$@")
        if [ "${#serial[@]}" -ne 0 ]; then
            go test "${flags[@]}" -tags "$GO_TAGS" "${serial[@]}" -short -p 1 -timeout=20m
        fi

        local -a parallel
        while IFS=$'\n' read -r pkg; do [ -n "$pkg" ] && parallel+=("$pkg"); done < <(parallel_test_packages "$@")
        if [ "${#parallel[@]}" -ne 0 ]; then
            go test "${flags[@]}" "${race_flags[@]}" -tags "$GO_TAGS" "${parallel[@]}" -short -timeout=20m
        fi
    }
}

# "go test" the provided packages and generate code coverage reports.
run_tests_with_coverage() {
    # run the tests serially
    time go test -p 1 -cover -coverprofile=profile_tmp.cov -tags "$GO_TAGS" "$@" -timeout=20m
    tail -n +2 profile_tmp.cov >> profile.cov && rm profile_tmp.cov
}

main() {
    # place the cache directory into the default build tree if it exists
    if [ -d "${GOPATH}/src/github.com/hyperledger/fabric/.build" ]; then
        export GOCACHE="${GOPATH}/src/github.com/hyperledger/fabric/.build/go-cache"
    fi

    # default behavior is to run all tests
    local package_spec=${TEST_PKGS:-github.com/hyperledger/fabric/...}

    # extra exclusions for ppc and s390x
    if [ "$(uname -m)" == "ppc64le" ] || [ "$(uname -m)" == "s390x" ]; then
        excluded_packages+=("github.com/hyperledger/fabric/core/chaincode/platforms/java")
    fi

    # when running a "verify" job, only test packages that have changed
    if [ "${JOB_TYPE}" = "VERIFY" ]; then
        # first check for uncommitted changes
        package_spec=$(packages_diff HEAD | grep -Ev '/vendor(/|$)')
        if [ -z "${package_spec}" ]; then
            # next check for changes in the latest commit
            package_spec=$(packages_diff HEAD^ | grep -Ev '/vendor(/|$)')
        fi
    fi

    # run everything when profiling
    if [ "${JOB_TYPE}" = "PROFILE" ]; then
        conditional_packages=()
    fi

    # expand the package spec into an array of packages
    local -a packages
    while IFS=$'\n'; read -r pkg; do [ -n "$pkg" ] && packages+=("$pkg"); done < <(list_and_filter "${package_spec}")
    while IFS=$'\n'; read -r pkg; do [ -n "$pkg" ] && packages+=("$pkg"); done < <(list_and_filter_conditional)

    if [ "${#packages[@]}" -eq 0 ]; then
        echo "Nothing to test!!!"
    elif [ "${JOB_TYPE}" = "PROFILE" ]; then
        echo "mode: set" > profile.cov
        run_tests_with_coverage "${packages[@]}"
        GO_TAGS="${GO_TAGS} pluginsenabled" run_tests_with_coverage "${plugin_packages[@]}"
        GO_TAGS="${GO_TAGS} pkcs11" run_tests_with_coverage "${pkcs11_packages[@]}"
        gocov convert profile.cov | gocov-xml > report.xml
    else
        run_tests "${packages[@]}"
        GO_TAGS="${GO_TAGS} pluginsenabled" run_tests "${plugin_packages[@]}"
        GO_TAGS="${GO_TAGS} pkcs11" run_tests "${pkcs11_packages[@]}"
    fi
}

main
